package org.liang.agile.command;


public class SleepCommand implements Command {

//    private  Logger logger = LoggerFactory.getLogger(this.getClass());
    
    private Command wakeupCommand;
    private ActiveObjectEngine engine;

    private long sleeptime;
    private long starttime;

    private boolean started;

    public SleepCommand(long milliseconds, ActiveObjectEngine engine, Command wakeupCommand) {
        this.sleeptime = milliseconds;
        this.engine = engine;
        this.wakeupCommand = wakeupCommand;
    }

    @Override
    public void execute() {
//        logger.info("execute sleep command");
        long currenttime = System.currentTimeMillis();
        if (!started) {
            started = true;
            starttime = currenttime;
            engine.addCommand(this);
        } else if ((currenttime - starttime) < sleeptime) {
            engine.addCommand(this);
        } else {
            engine.addCommand(wakeupCommand);
        }
    }

}
